package operator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import DBfun.DBconnection;

public class Operation 
{
	public Operation() 
	{
		
	}
	
	public boolean AddRigisterInfo(String nickname,String password)
	{
		try {
		    Connection con = DBconnection.getConnection();
		    Statement s = con.createStatement();
		     
		    String sql = "select * from accountinfo where nickname ='"+ nickname +"'";	  
		    
		    ResultSet rs =s.executeQuery(sql);
		    
		    if(rs.next())												
		    {
		    	DBconnection.closeConnection(s, con);
		    	return false;
		    }
		    else
		    {
		    	String asql = "insert into accountinfo(nickname,password)values('"+ nickname +"','"+ password +"')";
		    	boolean issuccess = s.execute(asql);
		    	DBconnection.closeConnection(s, con);
		    	return !issuccess;
		    }
		    
		} 
		catch (SQLException e) 
		{
		    System.out.println("SQL Exception: "+ e.toString());
		}
		return false;
	}

	public boolean checkloginaccount(String username,String password)
	{
		 
		    try 
		    {
		    	Connection con = DBconnection.getConnection();
				Statement s = con.createStatement();
				String sql = "select * from accountinfo where nickname ='"+ username +"'";

				ResultSet rs =s.executeQuery(sql);
				
				if(rs.next())
				{
					String userpassword = rs.getString(2);
					if(password.equals(userpassword))
					{
						DBconnection.closeConnection(s, con);
				    	return true;
					}
					else
					{
						DBconnection.closeConnection(s, con);
				    	return false;
					}
				}
				else
				{
					DBconnection.closeConnection(s, con);
			    	return false;
				}
			} 
		    catch (SQLException e) 
		    {
				e.printStackTrace();
			}
			return false;
		    
	}
	public String returnfriedlist(String nickname)
	{
		 
		    try 
		    {
		    	Connection con = DBconnection.getConnection();
				Statement s = con.createStatement();
				String sql = "select s.friendID from friendrelation s , friendrelation t where s.ownID = " +
						"t.ownID  and s.friendID = t.friendID and s.ownID = '"+ nickname +"'";
				ResultSet rs =s.executeQuery(sql);
				
				String temp = "";
				
				while(rs.next())
				{
					temp += rs.getString(1) + ",";

				}
				System.out.println(temp);
				return temp;
			} 
		    catch (SQLException e) 
		    {
				e.printStackTrace();
			}
			return null;
	}
	
	public boolean AddFriendIsIsExist(String friendID)
	{
		try {
		    Connection con = DBconnection.getConnection();
		    Statement s = con.createStatement();
		     
		    String sql = "select * from accountinfo where nickname ='"+ friendID +"'";	  
		    
		    ResultSet rs =s.executeQuery(sql);
		    
		    if(rs.next())											
		    {
		    	DBconnection.closeConnection(s, con);
		    	return true;
		    }
		    else
		    {
		    	DBconnection.closeConnection(s, con);
		    	return false;
		    }
		    
		} 
		catch (SQLException e) 
		{
		    System.out.println("SQL Exception: "+ e.toString());
		}
		return false;
	}
	
	public void addFriendToFriendList(String nickname,String friendID)
	{
		try {
		    Connection con = DBconnection.getConnection();
		    Statement s = con.createStatement();
		    String sql = "insert into friendrelation(friendID,ownID)values('"+ friendID +"','"+ nickname +"')";	
		    s.executeUpdate(sql);
		} 
		catch (SQLException e) 
		{
		    System.out.println("SQL Exception: "+ e.toString());
		}
	}
	
	public ArrayList<String> returnOfflineMassage(String nickname)
	{
		try {
		    Connection con = DBconnection.getConnection();
		    Statement s = con.createStatement();
		    ArrayList<String> omassage = new ArrayList<String>();
		    String sql = "select * from omassage where nickname = '"+ nickname +"'";
		    ResultSet rs =s.executeQuery(sql);
		    while(rs.next())
		    {
		    	String massage = rs.getString(2);
		    	omassage.add(massage);
		    }
		    return omassage;
		} 
		catch (SQLException e) 
		{
		    System.out.println("SQL Exception: "+ e.toString());
		}
		return null;
	}
	
	public void deleteOfflineMassage(String nickname)
	{
		try {
		    Connection con = DBconnection.getConnection();
		    Statement s = con.createStatement();
		    
		    String sql = "delete * from omassage where nickname = '"+ nickname +"'";
		    s.executeUpdate(sql);
		   
		} 
		catch (SQLException e) 
		{
		    System.out.println("SQL Exception: "+ e.toString());
		}
	}
	
	public void InsertOfflineMassage(String nickname,String massage)
	{
		try {
		    Connection con = DBconnection.getConnection();
		    Statement s = con.createStatement();
		    
		    String sql = "insert into omassage(nickname,massage)values('"+ nickname +"','"+ massage +"')";
		    s.executeQuery(sql);
		   
		} 
		catch (SQLException e) 
		{
		    System.out.println("SQL Exception: "+ e.toString());
		}
	}
}
